﻿using System.Text.RegularExpressions;

namespace IntypeSnippet2FingerText
{
	public class Snippet
	{
		public string TabTrigger { get; set; }
		public string Content { get; set; }

		public Snippet(string fileContent)
		{
			/*
			{
			content: 'return \$resVal;$0'
			title: 'return $resVal'
			scope: 'source.php'
			tab_trigger: 'retres'
			}
			*/

			var reg = new Regex(@"\t(\w+):\s'(.+?)'\n", RegexOptions.Singleline);

			var matches = reg.Matches(fileContent);

			this.TabTrigger = matches[3].Groups[2].Value;
			this.Content = matches[0].Groups[2].Value;

			this.Content = ConvertDollar(this.Content);
			this.Content = RemoveDollarZero(this.Content);
			this.Content = ReplaceToken(this.Content);
		}

		private string ConvertDollar(string content)
		{
			return content.Replace("\\$", "$");
		}

		private string RemoveDollarZero(string content)
		{
			return content.Replace("$0", "$[![]!]");
		}

		private string ReplaceToken(string content)
		{
			var reg = new Regex(@"\$\{\d{1}:(.+?)\}");

			return reg.Replace(content, @"$[![$1]!]");
		}
	}
}
